﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CommerceOne.Domains.Entities;
using CommerceOne.Domains.Repositories;

namespace CommerceOne.Domains.SqlRepositories
{
    public class SqlCommentRepository : ICommentRepository
    {
        private readonly EfDbContext _context = new EfDbContext();

        /// <summary>
        /// Totals this instance.
        /// </summary>
        /// <returns></returns>
        public int Total(long productId)
        {
            return this._context.Comments.Where(p=>p.ProductId == productId).Count();
        }

        /// <summary>
        /// Gets the comment.
        /// </summary>
        /// <param name="productId">The product id.</param>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns></returns>
        public List<Comment> GetComment(long productId, int page, int pageSize)
        {
            int skip = (page - 1)*pageSize;
            return this._context.Comments.Where(p => p.ProductId == productId).OrderByDescending(p => p.Date).Skip(skip).Take(pageSize).ToList();
        }

        /// <summary>
        /// Adds the comment.
        /// </summary>
        /// <param name="comment">The comment.</param>
        /// <returns></returns>
        public int AddComment(Comment comment)
        {
            this._context.Comments.Add(comment);
            return this._context.SaveChanges();
        }

    }
}
